// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async(event, context) => {
  const wxContext = cloud.getWXContext()
  var advices = {
    thinnish: "您的身体状况为【偏瘦】，建议增加体重。",
    healthy: "您的身体状况为【健康】，继续保持哦。",
    overweight: "您的身体状况为【过重】，建议适当减重。",
    fat: "您的身体状况为【肥胖】，强烈建议您减肥。"
  }
  var returnMsg = '';
  var weight = event.weight;
  var height = event.height;
  if (weight == 0 || weight == 0) {
    returnMsg = '计算错误！！输入值有0'
    return returnMsg;
  }
  //代表运算返回值，1、2、3、4分别对应偏瘦、健康、超重、肥胖
  var result = 0;
  var BMIdata = parseFloat((10000 * weight / (height * height)).toFixed(2)); //计算BMI,结果取小数点后两位
  if (BMIdata >= 0 && BMIdata < 18.5) {
    returnMsg = advices.thinnish;
    result = 1;
  } else if (BMIdata >= 18.5 && BMIdata < 24) {
    returnMsg = advices.healthy;
    result = 2;
  } else if (BMIdata >= 24 && BMIdata < 28) {
    returnMsg = advices.overweight;
    result = 3;
  } else if (BMIdata >= 28) {
    returnMsg = advices.fat;
    result = 4;
  } else {
    result = -1;
    returnMsg = '计算错误！！请输入正确的数值。';
  }
  var minWeight = 0;
  var maxWeight = 0;
  minWeight = (18.5 * (height * height) / 10000).toFixed(2); //最低体重
  maxWeight = (23.9 * (height * height) / 10000).toFixed(2); //最大体重

  return {
    weight,
    height,
    returnMsg,
    minWeight,
    maxWeight,
    BMIdata,
    result
  }
}